package com.zjl.core.controller;

import com.zjl.common.ResponseDate;
import com.zjl.common.UrlContent;
import com.zjl.core.entity.qo.UserLoginQo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author : zhangjl
 * @Description :
 * @Created : 2018-05-31 15:46
 **/
@Slf4j
@RequestMapping(UrlContent.BASEURL)
public class BaseLoginController {

    @RequestMapping(value = UrlContent.LOGIN, method = RequestMethod.POST)
    @ResponseBody
    public ResponseDate login(@RequestBody UserLoginQo qo){
        // 获取当前的Subject
        Subject subject = SecurityUtils.getSubject();
        // 调用Subject的isAuthenticated
        if (!subject.isAuthenticated()) {
            // 把用户名和密码封装为UsernamePasswordToken 对象
            UsernamePasswordToken token = new UsernamePasswordToken(qo.getAccount(), qo.getPassword());
            token.setRememberMe(true);
            try {
                // 执行登陆
                subject.login(token);
            } catch (AuthenticationException ae) {
                log.error("登录失败--->" + ae.getMessage());
            }
        }
        return new ResponseDate();
    }
}
